// pages/Publdetails/Publdetails.js
const {
  request
} = require("../../utils/index.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    artId: "", //帖子id
    isCollect:false,
    Detail: {}, //帖子详细信息
    phone: '18570487546',
    comments: [], //评论列表数据
    inputValue: "", //评论内容
    word: true, //评论展开
    show: false, //留言框显示
    min: 0,
    max: 500,
    currentWordNumber: '',
    Message: "请输入留言", //留言板内容
  },
  // 点击查看图片
  handleSwiper(e) {
    var that = this;
    var urlList = [];
    var scenePics = that.data.Detail.files
    for (var i = 0; i < scenePics.length; i++) {
      urlList.push(scenePics[i].updUrl);
    }
    wx.previewImage({
      urls: urlList,
      current: urlList[e.currentTarget.dataset.id]
    })
  },

  inputs: function (e) {
    var value = e.detail.value;
    var len = parseInt(value.length);
    if (len > this.data.max) return;
    this.setData({
      currentWordNumber: len
    });
  },
  onClickShow() {
    this.setData({
      show: true,
      isActive: true,
    });
  },

  onClickHide() {
    this.setData({
      show: false,
      isActive: false
    });
  },

  //获得焦点时清空多文本框
  qk(){
    console.log(1232)
    this.setData({
      Message:"",
      currentWordNumber: 0
    })
  },
  // 留言板内容
  Message(e) {
    this.setData({
      Message: e.detail.value
    })
    var value = e.detail.value;
    var len = parseInt(value.length);
    if (len > this.data.max) return;
    this.setData({
      currentWordNumber: len
    });
  },
  async messSub() {
    let that = this
    if (this.data.Message == "") {
      wx.showToast({
        title: '请输入留言',
        icon: "none"
      })
    } else {
      let par = {
        url: 'hw/portal/leaving/save?token=' + wx.getStorageSync('token'),
        data: {
          articleId: that.data.artId,
          articleTitle: that.data.Detail.title,
          lmContent: that.data.Message,
          lmModule: "business_wire"
        },
        method: 'POST',
        header: {
          'content-type': 'application/json', // 默认值
        },
      }
      var result = await request(par);
      if (result.status == 100) {
        wx.showToast({
          title: '留言成功！',
        })
        //提交后清空数据
        that.setData({
          Message: "",
          currentWordNumber: 0
        })
        this.onClickHide()
      } else {
        wx.showToast({
          title: result.message,
          icon: 'none'
        })
      }
    }
  },
  //评论展开或收起
  word: function () {
    this.setData({
      word: !this.data.word
    })
    console.log(this.data.word)
  },

      // 视频展示
      chooseshow: function (e) {
        var that = this;
        wx.previewMedia({
          current: 0,
          type: 'video',
          url: that.data.Detail.files[0].updUrl
        })
      },
  //商业共享详情页数据
  async init(artId) {
    try {
      let param = {
        url: 'hw/portal/articles/bw/detail/' + parseInt(artId) + "?token=" + wx.getStorageSync('token'),
        method: "get"
      }
      let res = await request(param);
      this.setData({
        Detail: res.data,
        phone: this.data.phone.substring(0, 3) + '****' + this.data.phone.substring(7) //手机号中间四位加密
        // phone:res.data.creatorPhone
      })
    } catch (error) {
      console.log(error)
    }
  },

  //评论
  async getpinglun() {
    var that = this;
    let par = {
      data: {
        cmtModule: 'business_wire', //需要查看评论对象
        // pageNum: 1, //页码
        // pageSize: 5, //每页条数
        objId: that.data.artId, //需要查看评论对象的ID
      },
      url: 'hw/portal/comment/page?token=' + wx.getStorageSync('token'),
      method: "get"
    }
    let res = await request(par);
    if (res.status == 100) {
      this.setData({
        comments: res.data.list
      })
    }
    console.log(this.data.comments)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //artId：商品发布帖ID
    let {
      artId
    } = options
    artId = parseInt(artId);
    this.setData({
      artId
    })
    this.init(artId);
    this.getpinglun();
  },

  //商业共享收藏增加
  syshoucang: async function (e) {
    let param = {
      url: "hw/portal/user/collect/save",
      header: {
        'content-type': 'application/x-www-form-urlencoded', // 默认值
      },
      data: {
        "token": wx.getStorageSync('token'),
        "collectModule": "business_wire",
        "objId": this.data.artId,
      },
      method: "POST"
    }
    let datainfo = await request(param);
    if (datainfo.status == 100) {
      this.setData({
        "Detail.isCollect": !this.data.isCollect
      })
    }
  },
  //商业共享收藏取消
  sycanceshoucang: async function () {
    let parm = {
      url: "hw/portal/user/collect/del",
      header: {
        'content-type': 'application/x-www-form-urlencoded', // 默认值
      },
      data: {
        "token": wx.getStorageSync('token'),
        "collectModule": "business_wire",
        "objId": this.data.artId, //当前用户id
      },
      method: "POST"
    }
    let datainfo = await request(parm);
    if (datainfo.status == 100) {
      this.setData({
        "Detail.isCollect": !this.data.isCollect
      })
      console.log(this.data.Detail.isCollect)
      console.log(this.data.isCollect)
    }
  },
  //评论内容
  blur(e) {
    console.log(e.detail.value)
    this.setData({
      inputValue: e.detail.value
    });
  },
  async enter() {
    let that = this;
    if (that.data.inputValue == "") {
      wx.showToast({
        title: '请填写评论内容',
        icon: "none",
      })
    } else {
      let param = {
        url: "hw/portal/comment/save",
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded', // 默认值
        },
        data: {
          "token": wx.getStorageSync('token'),
          cmtContent: that.data.inputValue,
          cmtModule: "business_wire",
          objId: that.data.artId, //当前用户id
        }
      }
      let data = await request(param);
      let list=[];
      if(that.data.comments){
        list=[...that.data.comments, data.data]
      }else{
        list.push(data.data)
      }
      if (data.status == 100) {
        that.setData({
          inputValue: "",
          comments: list
        })
        wx.showToast({
          title: '成功',
          icon: "none",
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})